import os
import sys

from twisted.application import service
from buildbot.slave.bot import BuildSlave

basedir = None
buildmaster_host = None
port = 9989
slavename = None
passwd = None
keepalive = 600
usepty = 0
umask = 022

try:
    from config import *
except ImportError:
    # The directory containing buildbot.tac is not in the path on Windows
    sys.path.append(os.path.dirname(__file__))
    from config import *

if slavename is None:
    msg = '*** No slavename configured in %s.\n' % repr(__file__)
    sys.stderr.write(msg)
    sys.exit(1)

if passwd is None:
    msg = '*** No passwd configured in %s.\n' % repr(__file__)
    sys.stderr.write(msg)
    sys.exit(1)

if buildmaster_host is None:
    msg = '*** No buildmaseter_host configured in %s.\n' % repr(__file__)
    sys.stderr.write(msg)
    sys.exit(1)

if port is None:
    msg = '*** No port configured in %s.\n' % repr(__file__)
    sys.stderr.write(msg)
    sys.exit(1)

if basedir is None:
    dir, _file = os.path.split(__file__)
    basedir = os.path.abspath(dir)

application = service.Application('buildslave')
s = BuildSlave(buildmaster_host, port, slavename, passwd, basedir,
               keepalive, usepty, umask=umask)
s.setServiceParent(application)
